package com.wontube.core.conf;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.google.gson.Gson;
import com.wontube.client.extension.utils.FileUtils;

/**
 * @author yuechao
 *
 */
public final class Domain implements Serializable {
	private static final long serialVersionUID = -4189426792940921414L;
	private static final Logger logger = LoggerFactory.getLogger(Domain.class);
	
	public static final String domainKey = "who";
	public static final String innerDomainKey = "innerWho";
	public static final Domain defaultDomain = new Domain();
	
	private String who = "/server";
	private String innerWho = "";
	private String[] cacheServers = new String[0];

	public String getWho() {
		return this.who;
	}
	
	public void setWho(String value) {
		this.who = value;
	}
	
	public String getInnerWho() {
		return this.innerWho;
	}
	
	public void setInnerWho(String value) {
		this.innerWho = value;
	}
	
	public String[] getCacheServers() {
		return this.cacheServers;
	}
	
	public void setCacheServers(String[] value) {
		this.cacheServers = value;
	}
	
	/**
	 * 初始化加载版本信息
	 */
	public void load(File file) {
		logger.info("begins loading domain config");

		String json = "";
		try {
			json = FileUtils.readAllText(file);
		} catch (IOException e) {
			logger.error(e.toString());
		}
		
		if ("".equals(json)) {
			logger.warn("failed loading domain config!");
			return;
		}
		
		Gson gson = new Gson();
		Domain domain = gson.fromJson(json, Domain.class);
		this.who = domain.who;
		this.innerWho = domain.innerWho;
		this.cacheServers = domain.cacheServers;

		logger.info("finished loading  domain config");
	}
}
